/**
 * Copyright (c) Jack Pien 2010
 */

#ifndef __DD_BRAIN_H
#define __DD_BRAIN_H

// Building brain
#define DD_BUILD_BRAIN

// Debug
#define DD_DEBUG_PRINT 1

//typedef unsigned long long uint64_t;
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t; 
//typedef long long int64_t;
typedef int int32_t;
typedef short int16_t;
//typedef char int8_t; 
typedef uint8_t boolean;
#define FALSE 0
#define TRUE 1

#include <stdint.h>
#include <stdio.h>
#include <sys/time.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h> 

#ifdef DD_BUILD_ROS
#include <daido_platform/ddxx_common.h>
#include <daido_platform/ddxx_msg.h>
#else
#include <ddxx_common.h>
#include <ddxx_msg.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

inline uint32_t ddGetMs() 
{
    struct timeval tv;
    suseconds_t tms;
    time_t tsec;
    uint32_t totalTime;

    gettimeofday( &tv, NULL );
    tms = tv.tv_usec*0.001;
    tsec = tv.tv_sec;
    tsec *= 1000;
    tsec += tms;
    totalTime = (uint32_t)(tsec);
    return totalTime;
}

inline uint32_t ddGetUs() 
{
    struct timeval tv;
    suseconds_t tus;
    time_t tsec;
    uint32_t totalTime;

    gettimeofday( &tv, NULL );
    tus = tv.tv_usec;
    tsec = tv.tv_sec;
    tsec *= 1000000;
    tsec += tus;
    totalTime = (uint32_t)(tsec);
    return totalTime;
}

#define DD_WAIT_US( us ) \
    { \
        uint32_t waitTill = 0; \
        volatile uint32_t www = 0; \
        waitTill = ddGetUs() + us; \
        while( ddGetUs() < waitTill ) { www++; } \
    }


// Sends drive command where -1.0 is full reverse, and 1.0 is full forward
extern void ddSendDriveScale( int32_t tty, float rwv, float lwv );

// Get an Ack command
extern uint32_t ddGetAck( int32_t tty, uint32_t* psender_ts, uint32_t* pack_ts );



#ifdef __cplusplus
}
#endif

#endif // __DD_BRAIN_H
